/*
 * GConsoleApp.java
 * Created on 28 sept. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.utils.misc;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.hockeo.vhbase.conf.GConfig;

/**
 * Base class for VH applications that should run on the console without a web and
 * application server.
 *
 * @version $Id$
 * @author jjanke
 */
public class GConsoleApp
{
  private AbstractApplicationContext d_appContext;
  private GConfig                    d_appCfg;

  /**
   * Initialises the application by instantiating the Spring container.
   *
   * @param strSpringContextFile the path and name of the Spring configuration file
   */
  public GConsoleApp( String strSpringContextFile )
  {
    System.out.println( "Initialization in progress..." );

    d_appContext = new FileSystemXmlApplicationContext( strSpringContextFile );
    d_appContext.registerShutdownHook();

    // Load application configuration (by convention called "appConfig")
    //
    d_appCfg = (GConfig) getBean( "appConfig" );

    System.out.print( d_appCfg.getProperty( "app_name" ) + " "
        + d_appCfg.getProperty( "app_version" ) + System.getProperty( "line.separator" ) );
  }

  /**
   * Returns a Spring Bean with the given name.
   *
   * @param strName the name of the Spring bean to instantiate.
   * @return the requested bean or <code>null</code>
   */
  public Object getBean( String strName )
  {
    return d_appContext.getBean( strName );
  }

  /**
   * Returns the Glacéo application configuration.
   */
  public GConfig getConfig()
  {
    return d_appCfg;
  }
}
